<?php
  class TambonService{
    	
  	function getTambonById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectDistrict){
		
		if(in_array($selectDistrict, $districtAccessId)){		
			$where = array();
			foreach($tambonAccessId as $id){
				$where[] = " pn_degis_contact.aco_tumbol_id = ".$id;
			}			
			$whereImplode = implode(' OR ', $where);
			
			$sql = "
							 SELECT 						 
								pn_degis_tumbol.atb_id,						 
								pn_degis_tumbol.atb_name,
								pn_degis_tumbol.atb_lat,
								pn_degis_tumbol.atb_lon,						 							 	
								COUNT(pn_degis_contact.aco_tumbol_id) AS count_population 							
							 FROM 
								samuidemap.pn_degis_tumbol,
								samuidemap.pn_degis_contact
							WHERE  
							( 
								$whereImplode
							)
							AND
								pn_degis_contact.aco_district_id = $selectDistrict	
							AND 
								pn_degis_contact.aco_tumbol_id = pn_degis_tumbol.atb_id									
							GROUP BY 
								pn_degis_contact.aco_tumbol_id
							ORDER BY 
								pn_degis_tumbol.atb_id";
			  //echo $sql."<br><br>";
				$column = array('id', 'name', 'lat', 'lon','count_population');
				$result = DBUtil::executeSQL($sql);
				$objectArray = DBUtil::marshallObjects ($result, $column);
				
				return $objectArray; 
				//$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray);
  		}else{
				echo '<?xml version="1.0" encoding="UTF-8"?>
							<datas>
								<data id=0>
									<title>Permission denied , you cannot access this district.</title>
								</data>
							</datas>';    		
  		}
  	}
  	
  	function arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray){

			$accessArray['province_access'] = implode(",", $provinceAccessId);
			$accessArray['district_access'] = implode(",", $districtAccessId);
			$accessArray['tambon_access'] = implode(",", $tambonAccessId);
  
         //Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;

      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );
      foreach($objectArray as $val){
        $tambolId = "tb".$val[id];
        $xml_node_data = $xml->createElement("data");
        $xml_node_datas->appendChild( $xml_node_data );
        $xml_node_data->setAttribute("id", $tambolId);
          $xml_node_title = $xml->createElement("title",$val[name]);
          $xml_node_data->appendChild( $xml_node_title );
          $xml_node_population = $xml->createElement("population",$val[count_population]);
          $xml_node_data->appendChild( $xml_node_population );
          $xml_node_latitude = $xml->createElement("latitude",$val[lat]);
          $xml_node_data->appendChild( $xml_node_latitude );
          $xml_node_longitude = $xml->createElement("longitude",$val[lon]);
          $xml_node_data->appendChild( $xml_node_longitude );
          $xml_node_span = $xml->createElement("span","0.2");
          $xml_node_data->appendChild( $xml_node_span );
          $xml_node_infopath = $xml->createElement("infopath","information.xml");
          $xml_node_data->appendChild( $xml_node_infopath );
          $xml_node_reportpath = $xml->createElement("reportpath","http://data.votethailand.com/report_no_data.xml");
          $xml_node_data->appendChild( $xml_node_reportpath );
          
          $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=village&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;st=".$val[id];
          
          $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);
          $xml_node_data->appendChild( $xml_node_xmlpath );
          $xml_node_xmlupdate = $xml->createElement("xmlupdate","LocalityData");
          $xml_node_data->appendChild( $xml_node_xmlupdate );
          $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
          $xml_node_data->appendChild( $xml_node_date );
      }

      return $xml;

  	}
  	
  	
  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	}    	
  }
?>